package cn.sjx.modules.nbiot.util;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.setting.dialect.Props;

/**
 * 自动从jar/resource或项目本身resource下获取配置文件，优先项目resource下，其次jar中
 *
 * @Author: sjx
 * @Date: 2019年09月03日 0003 14:10:40
 */
public class PropertiesUtil {

    private static final Log logger = LogFactory.get();

    private static final String PROPERTIES_PATH = "config.properties";

    /**
     * 获取resource下的配置文件
     */
    private static Props properties = null;

    /**
     * 获取资源目录信息
     *
     * @return
     */
    public static Props getProperties() {
        if (null == properties) {
            synchronized (PropertiesUtil.class) {
                if (null == properties) {
                    try {
                        // 优先resource下读取
                        properties = new Props(PROPERTIES_PATH);

                        logger.info("======> 【地磁|配置|读取】：本地读取 = {}", properties);
                    } catch (Exception e) {
                        logger.info("======> 【地磁|配置|读取】：错误信息 = {}", e.getMessage());
                    }
                }
            }
        }
        return properties;
    }

    /**
     * 根据key获取String值
     *
     * @param key
     * @return
     */
    public static String getStrValueByKey(String key) {
        return getProperties().getStr(key);
    }

    /**
     * 根据key获取int值
     *
     * @param key
     * @return
     */
    public static int getIntValueByKey(String key) {
        return getProperties().getInt(key);
    }

    /**
     * 根据key获取布尔值
     *
     * @param key
     * @return
     */
    public static boolean getBooleanValueByKey(String key) {
        return getProperties().getBool(key);
    }
}
